// pages/weather/weather.js
const app = getApp()
const api = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currIndex: 1,
    weatherList: [],
    isLoging: false,
    isScrollDown: false,
    isRefresher: false,//下拉刷新
    isColumns: false,//是否打开地区选择
    yj_levelsList: {
      "黄": "rgb(245,232,41)",
      "橙": "rgb(239,164,37)",
      "红": "#E44E4E",
      "白": "#000",
      "蓝": "rgb(39,39,237)"
    },
    columns: ['全部', '杭州', '宁波', '温州', '湖州', '嘉兴', '绍兴', '金华', '衢州', '舟山', '台州', '丽水'],
    nowCity: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navHeight: app.globalData.navHeight,
      scrollHeight: app.globalData.scrollHeight,
      scale: app.globalData.scale,
      listLoding: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getWeatherList();
  },

  //获取气象列表
  getWeatherList(id) {
    this.setData({
      listLoding: true
    })
    api.getWeather({
      "entity_unique_id": id && id != 3300 ? id : '',
      "currIndex": this.data.currIndex,
      "pageSize": "8"
    }).then((res) => {
      if (res.data.code === '1') return;
      //console.log(res.data.data);
      let time = setTimeout(() => {
        if (res.data.data.length == '0') {//判断是否到底
          this.setData({
            isScrollDown: true,
            isLoging: false,
            isRefresher: false,
            listLoding: false,
            weatherList: this.data.currIndex == '1' ? [] : this.data.weatherList,
          })
        } else {
          let list = res.data.data;
          this.setData({
            weatherList: this.data.currIndex == '1' ? list : this.data.weatherList.concat(list),
            isLoging: false,
            isRefresher: false,
            isScrollDown: false,
            listLoding: false
          })
        }
        clearTimeout(time);
      }, 300)
    })
  },

  //滑动到底部触发
  scrollDown() {
    if (this.data.isScrollDown) return;
    this.data.currIndex++;
    this.setData({
      isLoging: true
    })
    let dq = 3300 + this.data.nowCity;
    if (dq == 3300)
      dq = '';
    this.getWeatherList(dq);
  },


  //下拉刷新
  refresher() {
    this.data.currIndex = 1;
    this.data.isLoging = true;
    this.data.isScrollDown = false;
    this.data.isRefresher = true;
    let dq = 3300 + this.data.nowCity;
    if (dq == 3300)
      dq = '';
    this.getWeatherList(dq);
  },

  //修改城市
  onCityChange(event) {
    const { picker, value, index } = event.detail;
    console.log(this.data.nowCity);
    this.setData({
      isColumns: false,
      isScrollDown: false,
      currIndex: 1,
      nowCity: index
    })
    this.data.nowCity = index;
    console.log(3300 + index);
    this.getWeatherList(3300 + index);
  },


  //打开修改城市按钮
  onClickCity() {
    this.setData({
      isColumns: true
    })
  },

  //关闭城市按钮
  onClickHide() {
    this.setData({
      isColumns: false
    })
  }


})